#include "dicomimagelabel.h"
#include "../ImageData/seriesinstance.h"

#include "dcmtk/dcmdata/dcdeftag.h"

#include <QFrame>
#include <QMouseEvent>
#include <QDrag>
#include <QMimeData>
#include <QApplication>
#include <QLabel>
#include <QAction>

DicomImageLabel::DicomImageLabel(SeriesInstance *seriesPtr, QWidget *parent) :
    series(seriesPtr),
    nameLabel(new QLabel(this)),
    frameLabel(new QLabel(this)),
    QLabel(parent)
{
    setStyleSheet(QString::fromLatin1("background-color: rgb(0, 0, 0);"));
    setFrameShape(QFrame::Box);
    setFrameShadow(QFrame::Plain);
    setLineWidth(2);
    setAlignment(Qt::AlignCenter);
    setScaledContents(true);

    QPalette p = nameLabel->palette();
    p.setColor(QPalette::WindowText, Qt::white);
    nameLabel->setPalette(p);
    frameLabel->setPalette(p);
    QFont f = nameLabel->font();
    f.setPointSize(9);
    nameLabel->setFont(f);
    frameLabel->setFont(f);
}

DicomImageLabel::~DicomImageLabel()
{
    delete series;
}

bool DicomImageLabel::hasImage(const QString &file) const
{
    return series && series->hasImage(file);
}

bool DicomImageLabel::insertImage(ImageInstance *image)
{
    if (series && series->insertImage(image)) {
        if (series->getFrameCount(VT_XYPlane) == 1) {
            QPixmap pixmap;
            if (series->getPixmap(pixmap, VT_XYPlane))
                setPixmap(pixmap.scaled(IMAGE_LABEL_SIZE-2, IMAGE_LABEL_SIZE-2, Qt::KeepAspectRatio, Qt::SmoothTransformation));
            nameLabel->setText(series->getTagKeyValue(DCM_PatientName));
            nameLabel->move(1, 1);
        }
        QString tag = QString("%1: %2-%3").arg(series->getTagKeyValue(DCM_Modality),
                                               series->getTagKeyValue(DCM_SeriesNumber),
                                               QString::number(series->getFrameCount(VT_XYPlane)));
        frameLabel->setText(tag);
        QSize tagSize = frameLabel->sizeHint();
        frameLabel->resize(tagSize);
        frameLabel->move(IMAGE_LABEL_SIZE-tagSize.width(),
                         IMAGE_LABEL_SIZE-tagSize.height());
        return true;
    }
    return false;
}

bool DicomImageLabel::removeImage(const QString &imgFile)
{
    return series && series->removeImage(imgFile);
}

void DicomImageLabel::setHighlight(bool yes)
{
    QPalette p = palette();
    if (yes) p.setColor(QPalette::WindowText, Qt::green);
    else p.setColor(QPalette::WindowText, Qt::black);
    setPalette(p);
}

void DicomImageLabel::updateThumbnailImage()
{
    if (series) {
        QPixmap pixmap;
        if (series->getPixmap(pixmap, VT_XYPlane))
            setPixmap(pixmap.scaled(IMAGE_LABEL_SIZE-2, IMAGE_LABEL_SIZE-2, Qt::KeepAspectRatio, Qt::SmoothTransformation));
    }
}

void DicomImageLabel::mousePressEvent(QMouseEvent *e)
{
    emit imageClicked(this);
    dragOrg = e->pos();
    QLabel::mousePressEvent(e);
}

void DicomImageLabel::mouseMoveEvent(QMouseEvent *e)
{
    if ((e->buttons() & Qt::LeftButton) &&
            ((e->pos()-dragOrg).manhattanLength() > QApplication::startDragDistance())) {
        QDrag *drag = new QDrag(this);
        QMimeData *data = new QMimeData;
        data->setText(QString::number((qulonglong)series));
        drag->setMimeData(data);
        drag->exec(Qt::CopyAction);
    }
    QLabel::mouseMoveEvent(e);
}

void DicomImageLabel::mouseDoubleClickEvent(QMouseEvent *e)
{
    emit imageDoubleClicked(this);
    QLabel::mouseDoubleClickEvent(e);
}
